import {
	callFunc
} from "../../request.js"

const QQMapWX = require('../../js_sdk/map_sdk/qqmap-wx-jssdk.min.js')
const qqmapsdk = new QQMapWX({
	key: 'G2TBZ-DDIWX-3QT4Z-75MUV-XTMV6-5EFGQ'
})

export function getShareMan({
	showLoading = true,
	shareManId
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/getShareMan',
			data: {
				shareManId
			}
		}
	})
}

// 根据坐标获取城市区域码
export async function reverseGeocoder(location) {
	try {
		let foo = await new Promise((resolve, reject) => {
			qqmapsdk.reverseGeocoder({
				location: location || '',
				success: res => {
					resolve(res.result.ad_info.adcode.slice(0, 4))
				},
				fail: err => {
					console.error(err);
					uni.showToast({
						title: err.message,
						icon: 'none'
					})
					reject(err)
				}
			})
		})
		return foo
	} catch (e) {
		throw e
	}
}

// 根据区域码获取下属区域
export async function getCityDistrictList() {
	try {
		let userCityId = uni.getStorageSync('currentRegion').city.value + '00'

		let cityDistrictList = await new Promise((resolve, reject) => {
			qqmapsdk.getDistrictByCityId({
				id: userCityId,
				success: function(res) {
					// console.log('对应城市ID下的区县数据：', res.result[0])
					resolve(res.result[0])
				},
				fail: function(error) {
					console.error(error);
					uni.showToast({
						title: error.message,
						icon: 'none'
					})
					reject(error)
				}
			})
		})
		return cityDistrictList
	} catch (e) {
		throw e
	}
}

export function getNeareastCity({
	userLocation,
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'search/getNeareastCity',
			data: {
				userLocation
			}
		}
	})
}

export async function getCity() {
	let [err, loc] = await uni.getLocation({
		type: "gcj02"
	})
	if (!err) {
		let mapWX = require('../../js_sdk/map_sdk/qqmap-wx-jssdk.min.js')
		let mapSdk = new mapWX({
			key: 'G2TBZ-DDIWX-3QT4Z-75MUV-XTMV6-5EFGQ'
		})
		try {
			let userCity = await new Promise((resolve, reject) => {
				mapSdk.reverseGeocoder({
					location: {
						longitude: loc.longitude,
						latitude: loc.latitude
					},
					success: res => {
						console.log(JSON.parse(JSON.stringify(res)));
						resolve(res.result.ad_info.city)
					},
					fail: err => {
						console.log('获取定位城市失败：' + JSON.parse(JSON.stringify(
							err)));
						reject(err)
					}
				})
			})
			return {
				label: userCity
			}
		} catch (e) {
			throw e
		}
	}
}

export function receiveTicketGift({
	showLoading = true,
	giftId
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/receiveTicketGift',
			data: {
				giftId
			}
		}
	})
}

export function getGiftList({
	showLoading = true,
	pageNum = 1,
	pageSize = 10
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/getGiftList',
			data: {
				pageNum,
				pageSize
			}
		}
	})
}

export function getUserInfoById({
	showLoading = true,
	id
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'user/getUserInfoById',
			data: {
				id
			}
		}
	})
}

export function updateUserInfo({
	showLoading = true,
	code,
	wxUserInfo
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/updateUserInfoByWXData',
			data: {
				code,
				wxUserInfo
			}
		}
	})
}

export function getMyShareCode({
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/getMyShareCode'
		}
	})
}

export function exchangeGoods({
	showLoading = true,
	codeId,
	userId
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/exchangeGoods',
			data: {
				codeId,
				userId
			}
		}
	})
}

export function loginByWeiXin({
	showLoading = true,
	code,
	wxMobileData
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member/loginByWeixin',
			data: {
				code,
				wxMobileData
			}
		}
	})
}

export function loginWithPwd({
	showLoading = true,
	username,
	password
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member/loginWithPwd',
			data: {
				username,
				password
			}
		}
	})
}

export function updateMobile({
	showLoading = true,
	code,
	encryptedData,
	iv
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/updateMobileWithWXData',
			data: {
				code,
				encryptedData,
				iv
			}
		}
	})
}

export function getMyTeam({
	temporary,
	level,
	pageNum,
	pageSize,
	userId,
	keyword,
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/getMyTeamList',
			data: {
				temporary,
				level,
				pageNum: pageNum || 1,
				pageSize: pageSize || 20,
				userId,
				keyword,
			}
		}
	})
}

export function getMyAccount({
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'self/getAccount'
		}
	})
}

export function getMyHomeData({
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'client-get-my-home-data'
	})
}

export function registerShareExpert({
	codeId,
	codeCity,
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/registerShareExpert',
			data: {
				codeId,
				codeCity
			}
		}
	})
}

export function getWithdrawList(pageNum, pageSize, query, option) {
	return callFunc({
		...option,
		funcName: "client-get-withdraw-list",
		data: {
			token: uni.getStorageSync("token"),
			userType: "MEMBER",
			pageNum,
			pageSize,
			...query
		}
	})
}

export function applyWithdraw({
	showLoading = true,
	fee
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: "self/applyWithdraw",
			data: {
				fee
			}
		}
	})
}

export function checkToken({
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: "member/checkToken",
			data: {
				token: uni.getStorageSync("uni_id_token") || ""
			}
		}
	})
}

export function getPosterList({
	showLoading = true
}, currentCityCode) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/getMyPosters',
			data: {
				currentCityCode
			}
		}
	})
}

export function getSystemConfig(key, option) {
	return callFunc({
		...option,
		funcName: "client-get-system-config",
		data: {
			key
		}
	})
}

export function getShareGrade(userId, option) {
	return callFunc({
		...option,
		funcName: "client-get-share-grade",
		data: {
			userId
		}
	})
}

export function getHomeData({
	showLoading = true,
	userLocation,
	city
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member/getHomeData',
			data: {
				userLocation,
				city,
				token: uni.getStorageSync("uni_id_token")
			}
		}
	})
}
export function getUserPackageSearch({
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/getUserPackageSearch',
			data: {}
		}
	})
}

export function getPackageProperty({
	storeId,
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'member-self/getPackageProperty',
			data: {
				storeId
			}
		}
	})
}

export function getUserInfo({
	showLoading = true
}, currentCityCode) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/getMemberInfo',
			data: {
				currentCityCode
			}
		}
	})
}


export function getAccount({
	showLoading = true,
	codeId
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/getAccount',
			data: {
				codeId: codeId
			}
		}
	})
}

export function refreshQrcode({
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/refreshVipQrcode',
		}
	})
}

export function getVipCardList({
	showLoading = true,
	cityCode
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'vip-card/getVipCardList',
			data: {
				cityCode
			}
		}
	})
}

export function getMyMotorcade({
	showLoading = true,
	pageNum,
	pageSize,
	userLocation
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/getMyMotorcade',
			data: {
				pageNum,
				pageSize,
				userLocation
			}
		}
	})
}

export function getMyCollect({
	showLoading = true,
	collectType,
	pageNum,
	pageSize,
	userLocation
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/getMyCollect',
			data: {
				collectType,
				pageNum,
				pageSize,
				userLocation
			}
		}
	})
}

// 领取优惠券
export function receiveCoupon({
	showLoading = true,
	couponId
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/receiveCoupon',
			data: {
				couponId
			}
		}
	})
}


// 打开门禁
export function openDoor({
	doorSn,
	showLoading = true
}) {
	return callFunc({
		showLoading,
		funcName: 'uni-admin',
		data: {
			action: 'subscribe/openDoor',
			data: {
				doorSn
			}
		}
	})
}

//修改用户的个人信息
export function updateUserinfo({
	showLoading = true,
	userinfo
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/updateUserinfo',
			data: {
				userinfo
			}
		}
	})
}

//通过id获取用户的个人信息
export function getUserForId({
	showLoading = true,
	userId
}) {
	return callFunc({
		showLoading,
		funcName: "uni-admin",
		data: {
			action: 'member-self/getUserForId',
			data: {
				userId
			}
		}
	})
}